package com.jr9z.jrjz.controller;

import com.github.pagehelper.PageHelper;
import com.jr9z.jrjz.common.BaseResponse;
import com.jr9z.jrjz.common.ResponseBuilder;
import com.jr9z.jrjz.entity.MinistryCase;
import com.jr9z.jrjz.service.MinistryCaseService;
import com.jr9z.jrjz.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author: MrZcc
 * @Description:
 * @Date: Created in 10:43 2019-4-1
 * @Modified By:
 */

@Controller
@RequestMapping("/ministryCase")
public class MinistryCaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MinistryCaseService ministryCaseService;


    /**
     * 跳转到服务案例管理页面
     *
     * @return
     */
    @RequestMapping("/serviceManage")
    public String serviceCaseManage() {

        logger.info("进入服务案例管理");

        return "service/serviceCaseManage";
    }
    /**
     * 跳转到服务案例管理页面
     *
     * @return
     */
    @RequestMapping("/serviceAdd")
    public String serviceCaseAdd() {

        logger.info("进入服务案例新增");

        return "service/serviceCaseAdd";
    }

    /**
     * @Description:添加服务案例
     * @date: 10:51 2019-4-1
     * @param: ministryCase
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/addCase", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addMinistryCase(@RequestBody MinistryCase ministryCase) {
        ResponseBuilder custom = ResponseBuilder.custom();

        int result = 0;

        try {
            result = ministryCaseService.insertSelective(ministryCase);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }


    /**
     * @Description:根据服务类型id获取响应的案例列表
     * @date: 11:25 2019-4-1
     * @param: serviceId 服务类型id
     * @return: 以json格式返回数据
     */
    @RequestMapping(value = "/getServiceCaseByType", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getServiceCaseListByServiceId(Integer serviceId) {
        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            List<MinistryCase> ministryCaseList = ministryCaseService.getMinistryCaseByServiceId(serviceId);

            if (ministryCaseList != null) {
                custom.data(ministryCaseList).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * @Description:根据服务类型id获取响应的案例列表
     * @date: 11:25 2019-4-1
     * @param: serviceId 服务类型id
     * @return: 以json格式返回数据
     */
    @RequestMapping(value = "/admin/getServiceCaseByType", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetServiceCaseListByServiceId(Integer serviceId) {
        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            List<MinistryCase> ministryCaseList = ministryCaseService.getMinistryCaseByServiceId(serviceId);

            if (ministryCaseList != null) {
                custom.data(ministryCaseList).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }


    /**
     * @Description:根据服务类型id获取响应的案例列表(分页)
     * @date: 11:38 2019-4-1
     * @param: serviceId 服务类型id
     * @param: currPage  当前页
     * @param: pageSize  每页显示数量
     * @return: 以json格式返回数据
     */
    @RequestMapping(value = "/getServiceCasePage", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getServiceCaseListPage(@RequestParam Integer serviceId,
                                               @RequestParam Integer currPage,
                                               @RequestParam Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            List<MinistryCase> ministryCaseList = ministryCaseService.getMinistryCaseByServiceId(serviceId);

            Long count = ministryCaseService.getServiceCaseByServiceId(serviceId);

            if (ministryCaseList != null && count != 0) {
                custom.data(ministryCaseList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * @Description:根据服务类型id获取响应的案例列表(分页)
     * @date: 11:38 2019-4-1
     * @param: serviceId 服务类型id
     * @param: currPage  当前页
     * @param: pageSize  每页显示数量
     * @return: 以json格式返回数据
     */
    @RequestMapping(value = "/admin/getServiceCasePage", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetServiceCaseListPage(Integer serviceId,Integer currPage,Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            List<MinistryCase> ministryCaseList = ministryCaseService.getMinistryCaseByServiceId(serviceId);

            Long count = ministryCaseService.getServiceCaseByServiceId(serviceId);

            if (ministryCaseList != null && count != 0) {
                custom.data(ministryCaseList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * @Description:删除服务案例
     * @date: 11:44 2019-4-1
     * @param: caseId 案例id
     * @return: 响应结果以json格式返回
     */
    @RequestMapping(value = "/admin/deleteCase", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse deleteServiceCaseById(Integer caseId) {

        int result = 0;
        ResponseBuilder custom = ResponseBuilder.custom();
        try {
            result = ministryCaseService.deleteByPrimaryKey(caseId);

            custom = Constant.RESULT(result);
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:更新服务案例
     * @date: 11:55 2019-4-1
     * @param: ministryCase json格式的服务案例实体
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/updateCase", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateServiceCaseById(@RequestBody MinistryCase ministryCase) {
        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = ministryCaseService.updateByPrimaryKeySelective(ministryCase);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.ERROR_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:根据案例id查询案例详细信息
     * @date: 13:52 2019-4-1
     * @param: caseId 案例id
     * @return: 以json格式返回数据
     */
    @RequestMapping(value = "/getServiceById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getServiceCaseById(Integer caseId) {

        MinistryCase ministryCase;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            ministryCase = ministryCaseService.selectByPrimaryKey(caseId);

            if (ministryCase != null) {
                custom.data(ministryCase).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * @Description:根据案例id查询案例详细信息
     * @date: 13:52 2019-4-1
     * @param: caseId 案例id
     * @return: 以json格式返回数据
     */
    @RequestMapping(value = "/admin/getServiceById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetServiceCaseById(Integer caseId) {

        MinistryCase ministryCase;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            ministryCase = ministryCaseService.selectByPrimaryKey(caseId);

            if (ministryCase != null) {
                custom.data(ministryCase).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * 更新服务案例排序
     *
     * @param sortStr 操作字符（down---下移，up---上移）
     * @param caseId  案例id
     * @return
     */
    @RequestMapping(value = "/admin/updateSort", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateSort(String sortStr, Integer caseId,Integer serviceId) {

        ResponseBuilder custom = ResponseBuilder.custom();
        try {
            if (sortStr.equals("down")) {
                ministryCaseService.moveDown(caseId,serviceId);
            } else if (sortStr.equals("up")) {
                ministryCaseService.moveUp(caseId,serviceId);
            }

            custom.success(Constant.SUCCESS, Constant.SUCCESS_CODE);
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * 获取上一条记录
     *
     * @param serviceId 服务类型id
     * @param caseId  案例id
     * @return
     */
    @RequestMapping(value = "/getPrev", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getPrev(Integer caseId,Integer serviceId) {

        ResponseBuilder custom = ResponseBuilder.custom();

        MinistryCase ministryCase;
        try {

            ministryCase=ministryCaseService.getPrev(caseId,serviceId);


            custom.data(ministryCase).success(Constant.SUCCESS,Constant.SUCCESS_CODE);
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }

    /**
     * 获取下一条记录
     *
     * @param serviceId 服务类型id
     * @param caseId  案例id
     * @return
     */
    @RequestMapping(value = "/getNext", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getNext(Integer caseId,Integer serviceId) {

        ResponseBuilder custom = ResponseBuilder.custom();

        MinistryCase ministryCase;
        try {

            ministryCase=ministryCaseService.getNext(caseId,serviceId);


            custom.data(ministryCase).success(Constant.SUCCESS,Constant.SUCCESS_CODE);
        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }



}
